<template>
  <div id="Params">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-alert title="注意：只可以为第三级产品设置参数" type="warning" :closable="false" show-icon></el-alert>
      <el-row class="sp">
        <el-col>
          <span>选择商品分类：</span>
          <el-cascader
            v-model="seleteKeys"
            expand-trigger="hover"
            :options="cateList"
            :props="cateProps"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="isBtn">添加参数</el-button>
          <el-table :data="manyTableData" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope>
                <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="only">
          <el-button type="primary" :disabled="isBtn">添加属性</el-button>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {},
      cateList: [],
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      seleteKeys: [],
      activeName: "many",
      manyTableData: [],
      onlyTableData: []
    };
  },
  computed: {
    isBtn() {
      if (this.seleteKeys.length == 3) {
        return false;
      } else {
        return true;
      }
    },
    pId() {
      if (this.seleteKeys.length === 3) {
        return this.seleteKeys[2];
      } else {
        return null;
      }
    }
  },
  created() {
    this.getCataList();
  },
  methods: {
    async getCataList() {
      const { data: res } = await this.$http.get("categories");
      this.cateList = res.data;
    },
    handleChange() {
      if (this.seleteKeys.length != 3) {
        this.seleteKeys = [];
        return;
      }
      this.getParamData();
    },
    handleTabClick() {
      this.getParamData();
    },
    async getParamData() {
      const { data: res } = await this.$http.get(
        `categories/${this.pId}/attributes`,
        {
          params: { sel: this.activeName }
        }
      );
      if (this.activeName == "many") {
        this.manyTableData = res.data;
      } else {
        this.onlyTableData = res.data;
      }
      console.log(this.manyTableData);
    }
  }
};
</script>

<style lang="less" scoped>
.sp {
  margin-top: 20px;
}
</style>